/*
 *
 * Copyright (c) 2025 Bocloud Technologies Co., Ltd.
 * installer is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain n copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 */

package node

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"gopkg.openfuyao.cn/bkecommon/cluster/api/v1beta1"
)

var (
	tNodes = []v1beta1.Node{
		{
			Hostname: "node1",
			Role:     []string{"master", "worker"},
			IP:       "111",
		},
		{
			Hostname: "node2",
			Role:     []string{"master", "worker"},
			IP:       "222",
		},
	}
)

func TestCompareBKEConfigNode(t *testing.T) {
	tests := []struct {
		name    string
		args    []v1beta1.Node
		operate NodeOperate
		except  int
	}{
		{
			name: "create",
			args: []v1beta1.Node{
				{
					Hostname: "node1",
					Role:     []string{"master", "worker"},
					IP:       "111",
				},
				{
					Hostname: "node3",
					Role:     []string{"master", "worker"},
					IP:       "333",
				},
				{
					Hostname: "node5",
					Role:     []string{"master", "worker"},
					IP:       "666",
				},
			},
			operate: CreateNode,
			except:  2,
		},
		{
			name: "update",
			args: []v1beta1.Node{
				{
					Hostname: "node1",
					Role:     []string{"master", "worker"},
					IP:       "222",
				},
				{
					Hostname: "node3",
					Role:     []string{"master", "worker"},
					IP:       "333",
				},
			},
			operate: UpdateNode,
			except:  1,
		},
		{
			name: "delete",
			args: []v1beta1.Node{
				{
					Hostname: "node2",
					Role:     []string{"master", "worker"},
					IP:       "222",
				},
				{
					Hostname: "node3",
					Role:     []string{"master", "worker"},
					IP:       "333",
				},
			},
			operate: RemoveNode,
			except:  1,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got, ok := CompareBKEConfigNode(tNodes, tt.args); ok {
				var d int
				for _, g := range got {
					if g.Operate != tt.operate {
						continue
					}
					d++
				}
				assert.Equal(t, d, tt.except, "except %d, got %d", tt.except, d)
			} else {
				t.Errorf("except chanege %d, got %d", tt.except, len(got))
			}
		})
	}

}
